package com.igeekhome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeekhome.dto.ProblemDto;
import com.igeekhome.mapper.PProblemShipmentMapper;
import com.igeekhome.mapper.ProblemDtoMapper;
import com.igeekhome.mapper.ScanMapper;
import com.igeekhome.pojo.DNetwork;
import com.igeekhome.pojo.PProblemShipment;
import com.igeekhome.pojo.SysStaff;
import com.igeekhome.service.IProblemService;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.TableResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * @author 汪璇
 */
@Service
public class ProblemServiceImpl implements IProblemService {
    
    @Autowired
    private PProblemShipmentMapper pProblemShipmentMapper;
    
    @Autowired
    private ProblemDtoMapper problemDtoMapper;
    
    @Autowired
    private ScanMapper scanMapper;
    
    /**
    * @Description 问题件登记
    * @Param [p, waybillNumber]
    * @Return com.igeekhome.util.ResultBean
    **/
    @Transactional
    @Override
    public ResultBean proLogin(PProblemShipment p, Integer waybillNumber, DNetwork network, SysStaff staff) {
        // 设置回滚点
        Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
        try {
            p.setHanderId(staff.getStaffId()).setResponsiblePartyId(staff.getStaffId()).setRegistrantId(staff.getStaffId()).setHeaderNetwork(network.getNetworkId()).setWaybill(waybillNumber).setHeaderStatus(0);

            //这里的id是scan表的id
            scanMapper.updateStatus(p.getId(),23);
 
            //去掉scan标的id
            p.setId(null);
            
            pProblemShipmentMapper.insertSelective(p);
            return ResultBean.ok();
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);
            return ResultBean.fail("数据提交失败,已回滚");
        }

    }
    
    /**
    * @Description 问题件数据处理列表
    * @Param [network, page, limit]
    * @Return com.igeekhome.util.ResultBean
    **/
    @Override
    public TableResultBean proHanderList(DNetwork network, Integer page, Integer limit) {
        //开启分页
        PageHelper.startPage(page, limit);
    
        List<ProblemDto> problems = problemDtoMapper.selectByNetwork(network.getNetworkId(),1);
        //获取总条数
        long total = new PageInfo<ProblemDto>(problems).getTotal();
        return TableResultBean.ok(total,problems);
    }
    
    /**
    * @Description 问题件处理
    * @Param [pProblemShipment]
    * @Return com.igeekhome.util.ResultBean
    **/
    @Override
    public ResultBean proHander(PProblemShipment pProblemShipment) {
        if (pProblemShipment !=null){
            pProblemShipment.setHeaderStatus(1);
            pProblemShipmentMapper.updateByPrimaryKeySelective(pProblemShipment);
            return ResultBean.ok();
        }
       return ResultBean.fail("处理信息不能为空");
    }
    
    @Override
    public TableResultBean proQuery(DNetwork network, Integer page, Integer limit) {
        //开启分页
        PageHelper.startPage(page, limit);
    
        List<ProblemDto> problems = problemDtoMapper.selectByNetwork(network.getNetworkId(),1);
        //获取总条数
        long total = new PageInfo<ProblemDto>(problems).getTotal();
        return TableResultBean.ok(total,problems);
    }
}
